package com.shadow.demo.excel.demo.v2.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.shadow.demo.common.util.EnumUtil;
import com.shadow.demo.excel.demo.v2.converter.annotation.EnumClass;

public class StringEnumsConverter implements Converter<String> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.NUMBER;
    }

    /**
     * 根据自定义注解传入枚举类型，返回枚举成员名称.
     * @param context 写转换上下文
     **/
    @Override
    public WriteCellData<?> convertToExcelData(final WriteConverterContext<String> context) {
        final EnumClass enumClass = context.getContentProperty().getField().getAnnotation(EnumClass.class);
        return new WriteCellData<>(EnumUtil.getNameByValue(enumClass.value(), context.getValue()));
    }
}
